/**
 * tjftp -- A simple FTP server, using the ANSI C
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "lib/memory.h"
#include "plugin.h"

//如果你需要添加其他插件,它们的头文件添加在这里，并且在 tj_plugin_init 函数内关联它的回调函数
#include "plug/plug_filter.h"

static PPLUGIN_LIST plugin_list = NULL;
static void _add(PLUGIN_INIT init, PLUGIN_DESTORY destory, PLUGIN_LOOP_PRE loop_pre, PLUGIN_LOOP_POST loop_post, PLUGIN_CONNECTION connects, PLUGIN_DISCONNECT disconnect, PLUGIN_RECEIVE receive, PLUGIN_SEND send) {
	PPLUGIN_LIST sp_new = (PPLUGIN_LIST) tj_memory_alloc(sizeof(PLUGIN_LIST));
	if (!sp_new) {
		printf("memory allocate fail! \n");
		exit(-1);
	}
	sp_new->init = init;
	sp_new->destory = destory;
	sp_new->loop_pre = loop_pre;
	sp_new->loop_post = loop_post;
	sp_new->connects = connects;
	sp_new->disconnect = disconnect;
	sp_new->receive = receive;
	sp_new->send = send;
	sp_new->next = NULL;
	if (NULL == plugin_list) {
		plugin_list = sp_new;
	} else {
		PPLUGIN_LIST sp_last = plugin_list;
		while (sp_last->next != NULL) {
			sp_last = sp_last->next;
		}
		sp_last->next = sp_new;
	}
}
void tj_plugin_init(PSettings settings, int socket_fd) {
	tj_plugin_destory();
	//如果你想添加插件，测在这里初始化......
	_add(tj_plug_filter_init, tj_plug_filter_destory, tj_plug_filter_loop_pre, tj_plug_filter_loop_post, tj_plug_filter_connection, tj_plug_filter_disconnect, tj_plug_filter_receive, tj_plug_filter_send);
	//调用插件的初始化函数
	PPLUGIN_LIST current = plugin_list;
	while (current != NULL) {
		if (NULL != current->init) {
			(current->init)(settings, socket_fd);
		}
		current = current->next;
	}
}

void tj_plugin_destory() {
	PPLUGIN_LIST current;
	while (plugin_list != NULL) {
		current = plugin_list;
		plugin_list = plugin_list->next;
		if (NULL != current->destory) {
			(current->destory)();
		}
		tj_memory_free(current);
		current = NULL;
	}
	plugin_list = NULL;
}

void tj_plugin_loop_pre() {
	PPLUGIN_LIST current = plugin_list;
	while (current != NULL) {
		if (NULL != current->loop_pre) {
			(current->loop_pre)();
		}
		current = current->next;
	}
}

void tj_plugin_loop_post() {
	PPLUGIN_LIST current = plugin_list;
	while (current != NULL) {
		if (NULL != current->loop_post) {
			(current->loop_post)();
		}
		current = current->next;
	}
}

//这里的返回值，决定是否进入请求的分派, 如果通过，则返回 0 , 不通过，则返回 -1
int tj_plugin_connects(int accept_fd) {
	PPLUGIN_LIST current = plugin_list;
	while (current != NULL) {
		if (NULL != current->connects) {
			if ((current->connects)(accept_fd)) {
				return -1;
			}
		}
		current = current->next;
	}
	return 0;
}
void tj_plugin_disconnect(int accept_fd) {
	PPLUGIN_LIST current = plugin_list;
	while (current != NULL) {
		if (NULL != current->disconnect) {
			(current->disconnect)(accept_fd);
		}
		current = current->next;
	}
}

void tj_plugin_receive(char* buffer_recv) {
	PPLUGIN_LIST current = plugin_list;
	while (current != NULL) {
		if (NULL != current->receive) {
			(current->receive)(buffer_recv);
		}
		current = current->next;
	}
}
void tj_plugin_send(char* buffer_recv) {
	PPLUGIN_LIST current = plugin_list;
	while (current != NULL) {
		if (NULL != current->send) {
			(current->send)(buffer_recv);
		}
		current = current->next;
	}
}

